package com.six.elemo_manger.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.six.elemo_manger.R;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private  Fra_add Fra_add;
    private  Fra_online Fra_online;
    private  Fra_manger Fra_manger;
    private FragmentManager fragmentManager;
    private long mExitTime;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    setTabSelection(0);
                    return true;
                case R.id.navigation_dashboard:
                    setTabSelection(1);
                    setTitle("发布商品");
                    return true;
                case R.id.navigation_notifications:
                    setTabSelection(2);
                    setTitle("订单管理");
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        setTabSelection(0);
    }
    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        hideFragments(transaction);
        switch (index) {
            case 0:

                if (Fra_online == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    Fra_online = new Fra_online();
                    transaction.add(R.id.content, Fra_online);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(Fra_online);
                }
                break;
            case 1:

                if (Fra_add == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    Fra_add = new Fra_add();
                    transaction.add(R.id.content, Fra_add);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(Fra_add);
                }
                break;
            case 2:


                if (Fra_manger == null) {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    Fra_manger = new Fra_manger();
                    transaction.add(R.id.content, Fra_manger);
                } else {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(Fra_manger);
                }

                break;

            default:

                transaction.commit();
                break;
        }
        transaction.commit();

    }

    private void hideFragments(FragmentTransaction transaction) {
        if (Fra_online != null) {
            transaction.hide(Fra_online);
        }
        if (Fra_add != null) {
            transaction.hide(Fra_add);
        }
        if (Fra_manger != null) {
            transaction.hide(Fra_manger);
        }

    }


    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                if ((System.currentTimeMillis() - mExitTime) > 1000) {

                    Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {

                     System.exit(0);
                      finish();
                }
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

}
